package com.webdrider.servlet.action.snippet;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.common.model.abstracts.AbstractRequest;
import com.common.model.abstracts.AbstractValidator;
import com.common.model.validators.ActionValidator;
import com.common.security.abstracts.AbstractError;
import com.common.security.abstracts.AbstractException;
import com.common.security.errors.ParserError;
import com.common.security.exceptions.ManagerException;
import com.common.security.exceptions.ParserException;
import com.common.servlet.abstracts.AbstractParserServlet;
import com.common.servlet.interfaces.IParser;
import com.webdrider.manager.action.snippet.ArticleManager;
import com.webdrider.manager.action.snippet.DomainManager;
import com.webdrider.manager.interfaces.IArticleManager;
import com.webdrider.manager.interfaces.IDomainManager;
import com.webdrider.model.action.snippet.Article;
import com.webdrider.model.requests.snippet.AddSnippetArticleRequest;
import com.webdrider.model.response.snippet.AddSnippetArticleResponse;
import com.webdrider.model.validators.AddSnippetArticleValidator;
import com.webdrider.model.validators.SessionValidator;
import com.webdrider.security.errors.SnippetError;
import com.webdrider.security.errors.UserError;
import com.webdrider.security.exceptions.SnippetException;
import com.webdrider.servlet.util.RequestAction;
import com.webdrider.servlet.util.parser.snippet.AddSnippetArticleXMLParser;

public class AddSnippetArticleServlet extends AbstractParserServlet {
	private static final long serialVersionUID = 1L;
	private static Set<AbstractValidator> validators = new HashSet<AbstractValidator>();
	
	private AddSnippetArticleRequest requestContent;
	private AddSnippetArticleXMLParser parser = new AddSnippetArticleXMLParser();
	private IArticleManager articleManager = new ArticleManager();
	private IDomainManager domainManager = new DomainManager();
	
	static {
		validators.add(new ActionValidator(RequestAction.ADD_SNIPPET_ARTICLE));
		validators.add(new SessionValidator());
		validators.add(new AddSnippetArticleValidator());
	}
	
	@Override
	public void handle(HttpServletRequest request, HttpServletResponse response) {
		try {
			AddSnippetArticleResponse addSnippetArticleResponse = new AddSnippetArticleResponse();
			
			Article article = requestContent.getArticle();
			article.setDomain(domainManager.getDomain(requestContent.getArticle().getDomain().getValue()));
			articleManager.save(article);
			
			addSnippetArticleResponse.setRequestAction(requestContent.getAction());
			
			response.setContentType("text/xml");
			response.getWriter().write(parser().parseResponse(addSnippetArticleResponse));
			response.getWriter().close();
		} catch (ParserException parserException) {
			exception(parserException, response);
		} catch (IOException e) {
			error(new ParserError(ParserError.STREAM_ERROR), response);
		} catch (ManagerException managerException) {
			exception(managerException, response);
		} catch (SnippetException snippetException) {
			exception(snippetException, response);
		}
	}
	
	@Override
	public void setRequest(AbstractRequest abstractRequest) {
		requestContent = (AddSnippetArticleRequest) abstractRequest;
	}
	
	@Override
	public Set<AbstractValidator> getValidators() {
		return validators;
	}
	
	@Override
	public IParser parser() {
		return parser;
	}
	
	@Override
	public void exception(AbstractException exception, HttpServletResponse response) {
		super.exception(exception, response);
		
		switch (exception.getCode()) {
			 case ManagerException.DATABASE_EXCEPTION: {
				 error(new UserError(AbstractError.DATABASE_ERROR), response);
			 }
			 case SnippetException.DOMAIN_NOT_FOUND: {
				 error(new SnippetError(SnippetError.DOMAIN_NOT_FOUND), response);
			 }
			 case SnippetException.MULTIPLE_DOMAINS_FOUND: {
				 error(new UserError(AbstractError.DATABASE_ERROR), response);
			 }
		}
	}
}